package com.example.androidsurfacegarafika;

import com.aliyun.frontclient.FrontClientSurfaceTextureListener;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Relation;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.widget.RelativeLayout;

//https://github.com/google/grafika.git

public class MainActivity extends Activity {

    TextureView mTextureViews[] = new TextureView[3];
    RelativeLayout[] boxs = new RelativeLayout[3];
    FrontClientSurfaceTextureListener listeners[] = new FrontClientSurfaceTextureListener[3];

    private View.OnClickListener mViewListener = new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.start1:
            case R.id.start2:
            case R.id.start3:
            {
                if (mTextureViews[v.getId() - R.id.start1] == null) {
                    listeners[v.getId() - R.id.start1] = new FrontClientSurfaceTextureListener(MainActivity.this);
                    mTextureViews[v.getId() - R.id.start1] = new TextureView(MainActivity.this);
                    mTextureViews[v.getId() - R.id.start1].setSurfaceTextureListener(listeners[v.getId() - R.id.start1]);
                }
                if (!mTextureViews[v.getId() - R.id.start1].isAttachedToWindow()) {
                    boxs[v.getId() - R.id.start1].addView(mTextureViews[v.getId() - R.id.start1]);    
                }
            }
                break;
            case R.id.end1:
            case R.id.end2:
            case R.id.end3:
            {
                boxs[v.getId() - R.id.end1].removeAllViews();
                listeners[v.getId() - R.id.end1] = null;
            }
                break;
                
            case R.id.startRecord:
            {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName(
                        "com.example.androidsurfacegarafika",
                        "com.aliyun.frontserver.CoreService"));
                intent.putExtra("behavior", "startRecord");
                startService(intent);
            }
                break;
            case R.id.stopRecord:
            {
                Log.d("", "stopRecopfdskflafd");
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName(
                        "com.example.androidsurfacegarafika",
                        "com.aliyun.frontserver.CoreService"));
                intent.putExtra("behavior", "stopRecord");
                startService(intent);
            }
                break;
            default:
                break;
            }
        }
    };
    
    protected void onStart() {
        super.onStart();
    };
    
    protected void onStop() {
        super.onStop();
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boxs[0] = (RelativeLayout) findViewById(R.id.box1);
        boxs[1] = (RelativeLayout) findViewById(R.id.box2);
        boxs[2] = (RelativeLayout) findViewById(R.id.box3);
        
        
        findViewById(R.id.start1).setOnClickListener(mViewListener);
        findViewById(R.id.end1).setOnClickListener(mViewListener);
        findViewById(R.id.start2).setOnClickListener(mViewListener);
        findViewById(R.id.end2).setOnClickListener(mViewListener);
        findViewById(R.id.start3).setOnClickListener(mViewListener);
        findViewById(R.id.end3).setOnClickListener(mViewListener);
        
        findViewById(R.id.startRecord).setOnClickListener(mViewListener);
        findViewById(R.id.stopRecord).setOnClickListener(mViewListener);
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(
                "com.example.androidsurfacegarafika",
                "com.aliyun.frontserver.CoreService"));
        intent.putExtra("behavior", "openCamera");
        startService(intent);
    }
    
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName(
                "com.example.androidsurfacegarafika",
                "com.aliyun.frontserver.CoreService"));
        intent.putExtra("behavior", "releaseCamera");
        startService(intent);
    }
    
}
